package app.base.systemmanage.systemmenusmanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.UserSessionInfo;
import app.base.common.Utility;

import app.base.systemmanage.systemmenusmanage.dao.SysMenusDAO;
import app.base.systemmanage.systemmenusmanage.model.SysMenus;

public class SystemMenusLayerQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
	 
		String node = request.getParameter("node");
		String leaf = request.getParameter("leaf");
		String params = request.getParameter("params");
		String module = request.getParameter("module");
		
	    if (!(node == null || node.trim().length() == 0))
	    {
		    node = URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8).trim();
		    
			if (!(module == null))
			{
				module = URLDecoder.decode(module, Utility.CHARSET_ENCODING_UTF8).trim();
				node = node.substring(0, node.length() - module.length());
			}
	    	
			if (leaf == null || leaf.trim().length() == 0)
			{
				leaf = "2";
			}
			else
			{
				leaf = URLDecoder.decode(leaf, Utility.CHARSET_ENCODING_UTF8).trim();
			}
		
			if (params == null || params.trim().length() == 0)
			{
				params = "2";
			}
			else
			{
				params = URLDecoder.decode(params, Utility.CHARSET_ENCODING_UTF8).trim();
			}
			
			Map<String, Object> map = new HashMap<String, Object>();
			
			map.put("MENU_PARENT_ID", node);
			map.put("LEAF_NODE_FLAG", leaf);
			map.put("MENU_DISPLAY_FLAG", params);
			map.put("module", module);
			
		    out.println(getTreeSubNodesByNodeId(request, map).toString());
	    }
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	@SuppressWarnings("unchecked")
	private StringBuffer getTreeSubNodesByNodeId(HttpServletRequest request, Map<String, Object> map)
	{
		StringBuffer json = new StringBuffer("");
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysMenusDAO sysMenusDAO = (SysMenusDAO)context.getBean(Utility.SYSTEM_MENUS_MANAGE_MODULE_BEAN_NAME);
		
		List<SysMenus> list = sysMenusDAO.select_0001(map);
		
		json.append("[");

		for(int index = 0; index < list.size(); index++){
			SysMenus sysMenus = (SysMenus) list.get(index);
			
			String menuId = (map.get("module") == null) ? sysMenus.getMENU_ID().trim() : sysMenus.getMENU_ID().trim() + map.get("module").toString();
			String menuName = sysMenus.getMENU_NAME().trim();
			String menuDesc = sysMenus.getMENU_DESC() == null ? "" : sysMenus.getMENU_DESC().trim();
			String isLeaf = sysMenus.getLEAF_NODE_FLAG() == null ? "" : sysMenus.getLEAF_NODE_FLAG().trim();
			String normalIcon = sysMenus.getNORMAL_IMG_URL() == null ? "" : sysMenus.getNORMAL_IMG_URL().trim();
			String expandIcon = sysMenus.getEXPAND_IMG_URL() == null ? "" : sysMenus.getEXPAND_IMG_URL().trim();
			String navUrl = sysMenus.getMENU_NAV_URL() == null ? "" : sysMenus.getMENU_NAV_URL().trim();
			
			String menuParentId = sysMenus.getMENU_PARENT_ID().trim();
			String dispalyRankIndex = sysMenus.getDISPLAY_RANKING_INDEX().toString().trim();
			String menuDisplayFlag = sysMenus.getMENU_DISPLAY_FLAG().trim();
			String sysHoldFlag = sysMenus.getSYS_HOLD_FLAG().trim();

			String[] perms = {"PERM_ALL_PERMS", sysMenus.getMENU_ID().trim()};
			
			if (((String) map.get("MENU_DISPLAY_FLAG")).equals("1") && !UserSessionInfo.verifyUserPerms(request.getSession(), perms))
			{
				continue;
			}
			
			json.append("{id: '" + menuId + "',");
			json.append("text: '" + menuName + "',");
			if (!menuName.equals(""))
			{
				json.append("qtip: '" + menuDesc + "',");
			}
			if (isLeaf.equals("1"))
			{
				json.append("leaf: true,");
				json.append("actionModuleConfig: '" + navUrl + "',");
			}
			else
			{
				json.append("leaf: false,");
			}
			json.append("expandIcon: '" + expandIcon + "',");
			json.append("icon: '" + normalIcon + "',");
			json.append("menuParentId: '" + menuParentId + "',");
			json.append("displayRankIndex: '" + dispalyRankIndex + "',");
			json.append("menuDisplayFlag: '" + menuDisplayFlag + "',");
			json.append("sysHoldFlag: '" + sysHoldFlag + "',");
			json.append("singleClickExpand: true");

			json.append("}");
			
			if (index < list.size() - 1)
			{
				json.append(", ");
			}
		}
		json.append("]");
		
		return json;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
